﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollegeWebsite.Models;

namespace CollegeWebsite.Controllers
{
    public class StudentSubjectsController : Controller
    {
        //
        // GET: /StudentSubjects/
        CollegeDataContext _dataContext = new CollegeDataContext();
        public ActionResult Index()
        {
            if (Session["LoginStudent"] == null)
            {
                ViewData["ErrorInfo"] = "You do not have permission to access this regiion";
                return View("Error");
            }

            var student = (Student)Session["LoginStudent"];
            var studentSubject =
                _dataContext.StudentSubjects.Select(ss => ss).Where(ss => ss.Student == student).ToList();
            if (studentSubject.Count < 1)
            {
                ViewData["WarningInfo"] = "There are no content";
                return View("Error");
            }

            return View(studentSubject);
        }

        public ActionResult AddSubject()
        {
            var student = (Student)Session["LoginStudent"];
            List<Subject> subject = _dataContext.Subjects.Select(s => s).ToList();
            ViewData["subject"] = subject;

            StudentSubject ss = new StudentSubject();
            ss.StudentID = student.StudentID;
            return View(ss);

        }
        [HttpPost]
        public ActionResult AddSubject(StudentSubject ss)
        {
            try
            {
                // TODO: Add insert logic here
                _dataContext.StudentSubjects.InsertOnSubmit(ss);
                _dataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
